﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;

namespace Assets.Script
{
    class GameOverOnePanel:UIBase
    {
        public Image timeImg;

        float times = 5;
        public Button lookVideoBtn, noTankesBtn;
        public Text showTimeText;
        float showNoBtnTime = 1f;
        bool isme = false,ishowTime=true;

        public override void Init()
        {
            base.Init();
            lookVideoBtn.onClick.AddListener(() =>
            {
                StartCoroutine(PlayeVideo());
            });
            noTankesBtn.onClick.AddListener (() =>
            {
                UIManager.Ins.CloseUI(UIName.GameOverOnePanel);
                MessageCenter.Ins.BuredMess(MessId.GameOver, 0);
            });
        }
        IEnumerator PlayeVideo()
        {
            ishowTime = false;
            UIManager.Ins.OpenUI(UIName.AdvertisingPanel);
            yield return new WaitForSecondsRealtime(5.5f);
            UIManager.Ins.CloseUI(UIName.AdvertisingPanel);
            UIManager.Ins.CloseUI(UIName.GameOverOnePanel);
            MessageCenter.Ins.BuredMess(MessId.OpenLextObj);
            Time.timeScale = 1;
            ishowTime = true;
        }
        public override void Open()
        {
            base.Open();
            times = 5;
            showNoBtnTime = 1f;
            isme = true;
            noTankesBtn.gameObject.SetActive(false);
            ishowTime = true;
        }
        public override void Update()
        {
            base.Update();
            if (isme)
            {
                if(ishowTime)
                times -= Time.unscaledDeltaTime;
                timeImg.fillAmount = times / 5;
                showTimeText.text = Mathf.Ceil(times).ToString();
                showNoBtnTime -= Time.unscaledDeltaTime;
                if (showNoBtnTime <= 0)
                {
                    if (!noTankesBtn.gameObject.activeSelf)
                    {
                        noTankesBtn.gameObject.SetActive(true);
                    }
                }
                if (times <= 0)
                {
                    UIManager.Ins.CloseUI(UIName.GameOverOnePanel);
                    MessageCenter.Ins.BuredMess(MessId.GameOver, 0);
                }
            }
        }
    }
}
